package mobidever.godutch.database.sqlitedal;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import mobidever.godutch.database.base.SQLiteDALBase;
import mobidever.godutch.model.ModelPayout;
import mobidever.godutch.utility.DateTools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SQLiteDALPayout extends SQLiteDALBase {

	public SQLiteDALPayout(Context p_Context) {
		super(p_Context);
	}

	public Boolean InsertPayout(ModelPayout p_Info) {
		ContentValues _ContentValues = CreatParms(p_Info);
		Long p_NewID = getDatabase().insert("Payout", null, _ContentValues);
		p_Info.setmPayoutID(p_NewID.intValue());
		return p_NewID > 0;
	}
	
	public Boolean DeletePayout(String p_Condition)
	{
		return Delete(getTableNameAndPK()[0], p_Condition);
	}
	
	public Boolean UpdatePayout(String p_Condition, ModelPayout p_Info)
	{
		ContentValues _ContentValues = CreatParms(p_Info);
		return getDatabase().update("Payout", _ContentValues, p_Condition, null) > 0;
	}
	
	public Boolean UpdatePayout(String p_Condition,ContentValues p_ContentValues)
	{
		return getDatabase().update("Payout", p_ContentValues, p_Condition, null) > 0;
	}
	
	public List<ModelPayout> GetPayout(String p_Condition)
	{
		String _SqlText = "Select * From v_Payout Where  1=1 " + p_Condition;
		return getList(_SqlText);
	}
	
	protected ModelPayout FindModel(Cursor p_Cursor)
	{
		ModelPayout _ModelPayout = new ModelPayout();
		_ModelPayout.setmPayoutID(p_Cursor.getInt(p_Cursor.getColumnIndex("PayoutID")));
		_ModelPayout.setmAccountBookID(p_Cursor.getInt((p_Cursor.getColumnIndex("AccountBookID"))));
		_ModelPayout.setmAccountBookName((p_Cursor.getString(p_Cursor.getColumnIndex("AccountBookName"))));
		_ModelPayout.setmCategoryID(p_Cursor.getInt((p_Cursor.getColumnIndex("CategoryID"))));
		_ModelPayout.setmCategoryName((p_Cursor.getString(p_Cursor.getColumnIndex("CategoryName"))));
		_ModelPayout.setmPath((p_Cursor.getString(p_Cursor.getColumnIndex("Path"))));
		_ModelPayout.setmPayWayID(p_Cursor.getInt((p_Cursor.getColumnIndex("PayWayID"))));
		_ModelPayout.setmPlaceID(p_Cursor.getInt((p_Cursor.getColumnIndex("PlaceID"))));
		_ModelPayout.setmAmount(new BigDecimal(p_Cursor.getString(((p_Cursor.getColumnIndex("Amount"))))));
		Date _PayoutDate = DateTools.getDate(p_Cursor.getString(p_Cursor.getColumnIndex("PayoutDate")), "yyyy-MM-dd");	
		_ModelPayout.setmPayoutDate(_PayoutDate);
		_ModelPayout.setmPayoutType((p_Cursor.getString(p_Cursor.getColumnIndex("PayoutType"))));
		_ModelPayout.setmPayoutUserID((p_Cursor.getString(p_Cursor.getColumnIndex("PayoutUserID"))));
		_ModelPayout.setmComment((p_Cursor.getString(p_Cursor.getColumnIndex("Comment"))));
		Date _CreateDate = DateTools.getDate(p_Cursor.getString(p_Cursor.getColumnIndex("CreateDate")), "yyyy-MM-dd HH:mm:ss");	
		_ModelPayout.setmCreateDate(_CreateDate);
		_ModelPayout.setmState((p_Cursor.getInt(p_Cursor.getColumnIndex("State"))));
		
		return _ModelPayout;
	}

	@Override
	public void OnCreate(SQLiteDatabase p_DataBase) {
		StringBuilder s_CreateTableScript = new StringBuilder();
		
		s_CreateTableScript.append("Create  TABLE Payout(");
		s_CreateTableScript.append("[PayoutID] integer PRIMARY KEY AUTOINCREMENT NOT NULL");
		s_CreateTableScript.append(",[AccountBookID] integer NOT NULL");
		s_CreateTableScript.append(",[CategoryID] integer NOT NULL");
		s_CreateTableScript.append(",[PayWayID] integer");
		s_CreateTableScript.append(",[PlaceID] integer");
		s_CreateTableScript.append(",[Amount] decimal NOT NULL");
		s_CreateTableScript.append(",[PayoutDate] datetime NOT NULL");
		s_CreateTableScript.append(",[PayoutType] varchar(20) NOT NULL");
		s_CreateTableScript.append(",[PayoutUserID] text NOT NULL");
		s_CreateTableScript.append(",[Comment] text");
		s_CreateTableScript.append(",[CreateDate] datetime NOT NULL");
		s_CreateTableScript.append(",[State] integer NOT NULL");
		s_CreateTableScript.append(")");
		
		p_DataBase.execSQL(s_CreateTableScript.toString());
	}

	@Override
	public void OnUpgrade(SQLiteDatabase p_DataBase) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected String[] getTableNameAndPK() {
		return new String[]{"Payout","PayoutID"};
	}

	public ContentValues CreatParms(ModelPayout p_Info) {
		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("AccountBookID",p_Info.getmAccountBookID());
		_ContentValues.put("CategoryID",p_Info.getmCategoryID());
		_ContentValues.put("PayWayID",p_Info.getmPayWayID());
		_ContentValues.put("PlaceID",p_Info.getmPlaceID());
		_ContentValues.put("Amount",p_Info.getmAmount().toString());
		_ContentValues.put("PayoutDate",DateTools.getFormatDateTime(p_Info.getmPayoutDate(),"yyyy-MM-dd"));
		_ContentValues.put("PayoutType",p_Info.getmPayoutType());
		_ContentValues.put("PayoutUserID",p_Info.getmPayoutUserID());
		_ContentValues.put("Comment",p_Info.getmComment());
		_ContentValues.put("CreateDate",DateTools.getFormatDateTime(p_Info.getmCreateDate(),"yyyy-MM-dd HH:mm:ss"));
		_ContentValues.put("State",p_Info.getmState());
		
		return _ContentValues;
	}

}
